/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nextel.ui;

import com.nextel.ui.custom.OTextDecimalListener;
import javax.microedition.lcdui.Canvas;

/**
 *
 * @author Victor Laguna <vlaguna@dsbmobile.com>
 */
public class OEditableGrid extends OGrid implements OTextDecimalListener {
 
    public OEditableGrid(OGridInfo gridInfo) {
        super(gridInfo);
//        System.out.println("OEditableGrid");
    }

    /**
     * @author Victor Laguna <vlaguna@dsbmobile.com>
     */
    public void addRow(OGridRow row) {
        if (getTableListener() != null && !getTableListener().validarAgregar()) {
            return;
        }
        if (getTableListener() != null) {
            getTableListener().estadoAnterior();
        }

        int size = this.items.length + 1;
        OGridRow[] nitems = new OGridRow[size];
        int i = 0;
        for (i = 0; i < size - 1; i++) {
            nitems[i] = items[i];
        }
      
        nitems[size - 1] = row;
        this.items = nitems;

        nbrOfDisplayedLines = Math.min(items.length, nbrOfDisplayableLines);
        this.lineSelected = nbrOfDisplayedLines - 1;
        this.topLineIndex = Math.max(0, lineSelected - nbrOfDisplayedLines + 1);
        this.bottomLineIndex = this.items.length;
        this.columnSelected = 0;

        layOut(items);
        columnSelected = 0;
        if (getTableListener() != null) {
            getTableListener().filaAgregada();
        }
        if (getTableListener() != null) {
            getTableListener().estadoActual();
        }
        repaint();
    }
    //borra una fila de la tabla

    public int delRow() {
//        System.out.println("delRow");
        int valor = -1;

        if (getTableListener() != null && !getTableListener().validarEliminar()) {
            return valor;
        }

//        if (this.items.length < 2) {
//            return -1;
//        }

        int index = topLineIndex + lineSelected;
//        System.out.println("elimino fila-->:" + index);

        OGridRow[] other = new OGridRow[this.items.length - 1];

        int j = 0;
        for (int i = 0; i < this.items.length; i++) {
            if (i != index) {
                other[j++] = this.items[i];
            }
        }

        this.items = other;

        if (bottomLineIndex > this.items.length) {
            bottomLineIndex--;
        }
        if (this.items.length == 0) {
            topLineIndex = 0;
            bottomLineIndex = 0;
            lineSelected = 0;
        }


        layOut(items);
        columnSelected = 0;
        if (getTableListener() != null) {
            getTableListener().filaEliminada();
        }
        if (getTableListener() != null) {
            getTableListener().estadoActual();
        }
        repaint();

        return valor;
    }

    public void nuevaFila() {

        if (getTableListener() != null && !getTableListener().validarAgregar()) {
            return;
        }

        getTableListener().estadoAnterior();

//        OGridObjectRow row = new OGridObjectRow(3);
//
//
//        row.addEditableCell(0, "");
//
//        row.addEditableCell(1, "");
//
//        row.addCell(2, "");
//
//        addRow(row);
        if(getTableListener().buildFila()!= null)
            addRow( getTableListener().buildFila());
        
   

        getTableListener().filaAgregada();
        getTableListener().estadoActual();

    }

    protected synchronized void editarCelda(int numero) {

//        System.out.println("editarCelda" + numero );

        int currentSelectionIndex = topLineIndex + lineSelected;
        ///    System.out.println("TablaItem.editarCelda");

        if (!getTableListener().validateDataChange((char) numero)) {
            return;
        }

        OCell[] cells = items[currentSelectionIndex].getCells();

//        System.out.println("data = " + cells[columnSelected]);

        if (cells != null && cells[columnSelected] instanceof OEditableCell) {

            ((OEditableCell) cells[columnSelected]).keyPressed(numero);

            this.repaint();
        }

        if (getTableListener() != null) {
            getTableListener().dataChange();
        }

//        if (this.items.length > 1) {
//
//            OCell[] cells = items[currentSelectionIndex].getCells();
//            System.out.println("data = " + cells[columnSelected]);
//            if (cells != null && cells[columnSelected] instanceof OEditableCell) {
//                ((OEditableCell) cells[columnSelected]).keyPressed(numero);
//                this.repaint();
//            }

        getTableListener().dataChange();

    }

    public void keyPressed(int keyCode) {
        int currentSelectionIndex = topLineIndex + lineSelected;
//        System.out.println("OEditableGrid.keyPressed w/keyCode= " + keyCode);

        if (keyCode == BTN_OK) {
            nuevaFila();

        } else if (keyCode == BTN_NUMERAL) {

            //lamar metodo para borrar fila
            long currentTime = System.currentTimeMillis();

            if ((currentTime - lastTimeDeleteButtonPressed) <= milliSencodsToDeleteRow) {
//                System.out.println("eliminando");
                delRow();

                lastTimeDeleteButtonPressed = 0;
            } else {
                lastTimeDeleteButtonPressed = currentTime;
            }

        } else if (keyCode == OAbstractScreen.LEFT_KEY || keyCode == OAbstractScreen.RIGHT_KEY ||
                keyCode == OAbstractScreen.UP_KEY || keyCode == OAbstractScreen.DOWN_KEY) {
            super.keyPressed(keyCode);
        } else if (currentSelectionIndex < items.length) {

            if (getTableListener().isEditable()) {
                editarCelda((char) keyCode);
            }
        }
    }

    public void keyRepeated(int key) {
//        System.out.println("OEditableGrid.keyRepeated---" + key);
        if (key == Canvas.KEY_NUM1) {
            int currentSelectionIndex = topLineIndex + lineSelected;
            if (currentSelectionIndex < items.length) {
                if (getTableListener().isEditable()) {
                    editarCelda('.');
                }
            }
        }
    }
}
